// 布局加载器 - 加载通用头部和侧边栏
document.addEventListener('DOMContentLoaded', function() {
    loadLayout();
});

async function loadLayout() {
    try {
        // 加载头部
        const headerResponse = await fetch('layout/header.html');
        if (headerResponse.ok) {
            const headerHtml = await headerResponse.text();
            document.getElementById('header').innerHTML = headerHtml;
        }

        // 加载侧边栏
        const sidebarResponse = await fetch('layout/sidebar.html');
        if (sidebarResponse.ok) {
            const sidebarHtml = await sidebarResponse.text();
            document.getElementById('sidebar').innerHTML = sidebarHtml;
            
            // 设置当前页面的导航高亮
            setActiveNavigation();
        }
    } catch (error) {
        console.error('加载布局失败:', error);
    }
}

function setActiveNavigation() {
    const currentPage = window.location.pathname.split('/').pop() || 'dashboard.html';
    const navLinks = document.querySelectorAll('.sidebar-nav .nav-link');
    
    navLinks.forEach(link => {
        link.classList.remove('active');
        const href = link.getAttribute('href');
        if (href === currentPage || (currentPage === 'index.html' && href === 'dashboard.html')) {
            link.classList.add('active');
        }
    });
}